package xjtu.sad.zbsj.dao.manageTask;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import xjtu.sad.zbsj.task.annotater.manageTask.DrawContent;
import xjtu.sad.zbsj.task.annotater.manageTask.ManageTask;
import xjtu.sad.zbsj.task.annotater.manageTask.TaskMark;

import java.util.Date;
import java.util.List;

@Mapper
public interface ManageTaskDao {
    @Select("select task_id,task_title,user_name,task_kind,task_publish_time,task_award,task_limit_time,task_require,task_desc,task_status from t_task,t_user_info where t_user_info.user_id=t_task.publisher ")
    List<ManageTask> getTaskList();

    @Select("select draw_id,task_title,user_name,task_kind,task_award,start_time,end_time,\n" +
            "task_limit_time,task_require,task_desc,draw_status\n" +
            "from t_draw , t_task,t_user_info\n" +
            "where t_draw.user_id=#{userId} and t_draw.task_id=t_task.task_id and t_user_info.user_id=t_task.publisher ")
    List<ManageTask> getMyTask(Integer userId);

    @Select("select task_status from t_task where task_id=#{task_id}")
    Integer getTaskStatus(Integer task_id);

    @Select("select task_max_distribution from t_task where task_id=#{task_id}")
    Integer getTaskMaxDistribute(Integer task_id);

    @Select("select task_mark_count from t_task where task_id=#{task_id}")
    Integer getTask_mark_count(Integer task_id);

    @Insert("insert into t_draw(task_id,start_time,user_id) values(#{task_id},#{start_time},#{userId})")
    void insertDrawTaskIni(Integer task_id, Date start_time, Integer userId);

    @Select("select draw_id from t_draw where user_id=#{userId} and task_id=#{task_id} and start_time=#{start_time}")
    Integer selectTaskDrawId(Integer task_id, Integer userId, Date start_time);

    List<DrawContent> getDrawDistributeTimes(Integer task_id, Integer times, List<Integer> userMarkList, int size);

    void updateDistributeTimes(List<DrawContent> drawContents, Integer times);

    @Update("update t_draw set draw_num=#{alreadyGet} where draw_id=#{taskDrawId}")
    void updateDrawTaskTimes(Integer taskDrawId, Integer alreadyGet);

    void insertMark(Integer task_id, Integer userId, Integer taskDrawId, List<DrawContent> insertContentList);

    @Update("update t_task set task_status=2 where task_id=#{task_id}")
    void updateTaskStatus(Integer task_id);

    @Select("select count(*) from t_mark where task_id =#{task_id}")
    Integer getDistributeCount(Integer task_id);

    @Select("select content_id from t_mark where user_id = #{userId} and task_id = #{taskId}")
    List<Integer> getUserMarkList(Integer userId, Integer taskId);

    @Select("select * from t_draw where task_id = #{taskId} and user_id = #{userId}")
    List<DrawContent> getDrawContentList(Integer userId, Integer taskId);

    @Select("select content_id,mark_id,start_time,end_time,warning,status as mark_status,draw_id from t_mark where draw_id=#{draw_id}")
    List<TaskMark> getDetail(Integer draw_id);

    @Select("select count(*) from t_task")
    String getTaskListPageSum();

    @Select("select count(*) from t_draw where user_id=#{userId}")
    String getMyTaskPageSum(Integer userId);

    @Select("select count(*) from t_mark where draw_id=#{draw_id}")
    String getMarkPageSum(Integer draw_id);

    @Select("select count(*) from t_mark where task_id=#{task_id}")
    Integer drawNum(Integer task_id);

    @Select("select unpass_reason from t_audit where mark_id = #{markId}")
    String getUnPassReason(Integer markId);
}

